import { configureStore, combineReducers } from '@reduxjs/toolkit'
import { useDispatch } from 'react-redux'
import {
  persistStore,
  persistReducer,
  // FLUSH,
  // REHYDRATE,
  // PAUSE,
  // PERSIST,
  // PURGE,
  // REGISTER,
} from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import usersReducer from './modules/users'
import signsReducer from './modules/signs'
import newsReducer from './modules/news'
import checksReducer from './modules/checks'

const persistConfig = {
  key: 'root',
  version: 1,
  storage,
  whitelist: ['token'],
}

const rootReducer = combineReducers({
  users: persistReducer(persistConfig, usersReducer),
  signs: signsReducer,
  news: newsReducer,
  checks: checksReducer,
})

const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false,
    }),
})

persistStore(store)

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch
export const useAppDispatch: () => AppDispatch = useDispatch

export default store
